import java.io.File;
import java.io.FileOutputStream;

/*
 * User Standard Deviation
 */
public class AUSD extends Scheduler implements Runnable {
	int start;
	int stop;
	byte[] BLOCK;

	public AUSD(int start, int stop) {
		this.start = start;
		this.stop = stop;
	}

	void writeBlock() {
		String FILE_NAME = String.format("%s%07d.bin", BLOCK_PATH, start);		File out = new File(FILE_NAME);
		FileOutputStream fos;
		try {
			fos = new FileOutputStream(out);
			fos.write(BLOCK);
		} catch (Exception e) {
			e.printStackTrace();
		}

		System.out.println("Done writing: " + FILE_NAME + " @ "
				+ this.getClass());
	}

	@Override
	public void run() {
		int ilen = USERB[0].ilen;
		BLOCK = new byte[(stop - start) * ilen];

		int c = 0;
		for (int bUserID = start; bUserID < stop; bUserID++) {
			byte[] votes = USERA[bUserID].votes;
			int[] items = USERA[bUserID].items;
			int uilen = USERA[bUserID].ilen;
			
			byte pred = 0;
			double sum = 0;
			for (int i = 0; i < uilen; i++) {
				double v = (((double) votes[i]) - ITEMA[items[i]].mean);
				sum += v*v;
			}
			pred = (byte) Math.sqrt((sum/uilen));
			for (int i = 0; i < ilen; i++) {
				BLOCK[c * ilen + i] = pred;
			}
			c++;
		}

		writeBlock();
	}
}
